package org.maze;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.LCD;

public class PCControl_nxt {
	
	private static DataInputStream dis = null;
	private static DataOutputStream dos = null;
	
	private static int directionCommand = Direction.NODIRECTION;
	
	public static void main(String[] args) {
		
		RobotModel robot = new RobotModel();
		
		LCD.drawString("Waiting.....", 0, 0);
		
		ConnectorNXT connector = new ConnectorNXT();
		connector.waitForConnection();
		
		LCD.drawString("Connected.....", 0, 0);
		
		dis = connector.getDIS();
		dos = connector.getDOS();
		
		while(true) {
			
			try {
				directionCommand = dis.readInt();
			}
			catch(IOException ex) {
				LCD.clear();
				LCD.drawString("error reading direction command", 0, 0);
				System.exit(-1);
				
			}
			
			if (directionCommand != Direction.NODIRECTION) {
				robot.gotoDirection(directionCommand);
				LCD.drawString("Front:" + robot.getFrontSensorValue()+"...", 0, 3);
				LCD.drawString("Left:" + robot.getLeftSensorValue()+"...", 0, 4);
				LCD.drawString("Right:" + robot.getRightSensorValue()+"...", 0, 5);
				
				int lineCounter = 0;
				
				if (robot.isFrontWall()) {
					LCD.drawString("FrontWall...", 0, lineCounter+6);;
					lineCounter++;
				}
				
				if (robot.isLeftWall()) {
					LCD.drawString("LeftWall...", 0, lineCounter+6);;
					lineCounter++;
				}
				
				if (robot.isRightWall()) {
					LCD.drawString("LeftWall...", 0, lineCounter+6);;
					lineCounter++;
				}
			}
			
			try {
				dos.writeInt(State.READY);
				dos.flush();
			}
			catch(IOException ex) {
				LCD.clear();
				LCD.drawString("error writing state", 0, 0);
				System.exit(-1);
			}
			
		}
	}
}
